package org.iabako.client.ui.page.business.service;

import com.google.gwt.core.client.GWT;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.user.client.ui.Composite;
import com.google.gwt.user.client.ui.HTMLPanel;
import org.iabako.client.config.page.PageConfigSearchEnum;
import org.iabako.client.config.page.PageEnum;
import org.iabako.client.navigation.NavigationController;
import org.iabako.client.tools.DefaultSuggestHandler;
import org.iabako.client.ui.form.business.service.ServiceSearchForm;
import org.iabako.client.ui.layout.field.Button;
import org.iabako.client.ui.layout.widget.datatable.DataTable;
import org.iabako.client.ui.page.AbstractSearchPage;
import org.iabako.client.user.AuthenticateUserProvider;
import org.iabako.shared.entity.enumeration.TrackingType;
import org.iabako.shared.iservice.business.ServiceServiceAsync;
import org.iabako.shared.iservice.generic.CRUDServiceAsync;
import org.iabako.shared.iservice.generic.SearchServiceAsync;
import org.iabako.shared.iservice.generic.SuggestServiceAsync;

/**
 * TODO javadoc
 * Date: 24/11/13
 * Time: 17:15
 */
public class ServiceSearch extends AbstractSearchPage {

    interface MyUiBinder extends UiBinder<HTMLPanel, ServiceSearch> {
    }

    private static final MyUiBinder binder = GWT.create(MyUiBinder.class);

    @UiField
    HTMLPanel serviceSearchPanel;
    @UiField
    HTMLPanel resultsPanel;

    public ServiceSearchForm serviceSearchForm;

    public ServiceSearch() {
        super();

        Button createService = new Button("pageactionbutton", "service_create_title");
        page.actionButtons.add(createService);

        createService.getButton().addClickHandler(new ClickHandler() {
            public void onClick(ClickEvent event) {
                NavigationController.getInstance().goTo(PageEnum.ServiceCreate);
            }
        });
    }

    @Override
    protected HTMLPanel getContentPanel() {
        return binder.createAndBindUi(this);
    }

    @Override
    protected SearchServiceAsync getSearchServiceAsync() {
        return ServiceServiceAsync.Util.getInstance();
    }

    @Override
    protected CRUDServiceAsync getCRUDServiceAsync() {
        return ServiceServiceAsync.Util.getInstance();
    }

    @Override
    protected SuggestServiceAsync getSuggestServiceAsync() {
        return ServiceServiceAsync.Util.getInstance();
    }

    @Override
    protected HTMLPanel getResultsPanel() {
        return resultsPanel;
    }

    @Override
    protected HTMLPanel getSearchPanel() {
        return serviceSearchPanel;
    }

    @Override
    protected Composite getSearchForm(DataTable dataTable) {
        if (serviceSearchForm == null) {
            serviceSearchForm = new ServiceSearchForm(new DefaultSuggestHandler(getSuggestServiceAsync()), dataTable);
        }
        return serviceSearchForm;
    }

    @Override
    protected PageConfigSearchEnum getSearchPageConfigEnum() {
        return PageConfigSearchEnum.ServiceSearch;
    }

    @Override
    protected PageEnum getModificationPage() {
        return PageEnum.ServiceModify;
    }

    @Override
    protected void dataTableRemoveCustomAction() {
        AuthenticateUserProvider.getTrackingPanel().refresh(TrackingType.serviceDelete);
    }
}
